importClass(android.app.AlertDialog);
importClass(android.app.ProgressDialog);
importClass(android.content.DialogInterface);
importClass(android.widget.EditText);

importClass(android.app.DatePickerDialog)
importClass(android.app.TimePickerDialog)
importClass(android.app.TimePickerDialog.OnTimeSetListener)

// importClass(android.app.DatePickerDialog.OnDateSetListener)

let dialog = {};

/**
 * 
 * @param {*普通对话框标题} title 
 * @param {*普通对话框消息} message 
 * @param {*普通对话框是否可以强制关闭} cancelable 
 * @param {*第一个按钮对象: {text:"按钮名称", fun : () => {log("点击该按钮之心函数")}}} bu1 
 * @param {*第二个按钮对象: {text:"按钮名称", fun : () => {log("点击该按钮之心函数")}}} bu2 
 * @param {*第三个按钮对象: {text:"按钮名称", fun : () => {log("点击该按钮之心函数")}}} bu3 
 */
dialog.normal = function (title, message, cancelable, bu1, bu2, bu3) {
    if (cancelable == null) cancelable = true;
    var dlog = new AlertDialog.Builder(activity).setCancelable(cancelable).setTitle(title).setMessage(message);
    if (bu1) {
        dlog.setPositiveButton(bu1.text, new DialogInterface.OnClickListener({
            onClick: function () {
                if (bu1.fun) {
                    bu1.fun();
                }
            }
        }));
    }
    if (bu2) {
        dlog.setNegativeButton(bu2.text, new DialogInterface.OnClickListener({
            onClick: function () {
                if (bu2.fun) {
                    bu2.fun();
                }
            }
        }));
    }
    if (bu3) {
        dlog.setNeutralButton(bu3.text, new DialogInterface.OnClickListener({
            onClick: function () {
                if (bu3.fun) {
                    bu3.fun();
                }
            }
        }));
    }
    dlog.show();
}

/**
 * 
 * @param {*列表对话框标题} title 
 * @param {*列表对话框数据} listData 
 * @param {*列表对话框是否可以强制关闭} cancelable 
 * @param {*点击某个项目后执行的函数，第二个参数是索引} fun 
 */
dialog.list = function (title, listData, cancelable, fun) {
    if (cancelable == null) cancelable = true;
    var dlog = new AlertDialog.Builder(activity).setCancelable(cancelable).setTitle(title).setItems(listData, new DialogInterface.OnClickListener({
        onClick: function (dlog, which) {
            if (fun) fun(dlog, which);
        }
    }));

    dlog.show();

}

/**
 * 
 * @param {*单选对话框标题} title 
 * @param {*单选对话框数据数组} listData 
 * @param {*是否可以强制关闭} cancelable 
 * @param {*点击确定之后执行的函数， 第二个参数是索引} fun 
 */
dialog.radio = function (title, listData, cancelable, fun) {
    let yourChoice = -1;
    if (cancelable == null) cancelable = true;


    var dlog = new AlertDialog.Builder(activity).setCancelable(cancelable).setTitle(title).setSingleChoiceItems(listData, 0, new DialogInterface.OnClickListener({
        onClick: function (dlog, which) {
            yourChoice = which;
        }
    }));

    dlog.setPositiveButton('确定', new DialogInterface.OnClickListener({
        onClick: function (dlog, which) {
            if (yourChoice != -1) {
                if (fun) fun(dlog, yourChoice);
            } else {
                console.error('-1');
            }
        }
    }));

    dlog.show();

}

/**
 * 
 * @param {*多选对话框标题} title 
 * @param {*多选对话框数据数组} listData 
 * @param {*多选对话框初始选择状态数组} checkeds 
 * @param {*多选对话框是否可以强制关闭} cancelable 
 * @param {*点击确定后执行的函数, 第三个参数是对话框选择的数据数组} fun 
 */
dialog.checkbox = function (title, listData, checkeds, cancelable, fun) {
    let yourChoices = [];
    if (cancelable == null) cancelable = true;
    if (checkeds == null || checkeds.length != listData.length) {
        checkeds = [];
        listData.forEach(() => {
            checkeds.push(false);
        });
    }

    let dlog = new AlertDialog.Builder(activity).setCancelable(cancelable).setTitle(title).setMultiChoiceItems(listData, checkeds, new DialogInterface.OnMultiChoiceClickListener({
        onClick: function (dlog, which, isChecked) {
            if (isChecked) {
                yourChoices.push(listData[which]);
            }
        }
    }));

    dlog.setPositiveButton('确定', new DialogInterface.OnClickListener({
        onClick: function (dlog, which) {
            if (fun) fun(dlog, which, yourChoices);

        }
    }));

    dlog.show();

}

/**
 * 
 * @param {*加载对话框标题} title 
 * @param {*加载对话框消息} message 
 * @param {*是否可以强制关闭加载对话框} cancelable 
 * @returns 返回加载对话框对象 可以用来调用关闭对话框的函数
 */
dialog.loading = function (title, message, cancelable) {
    if (cancelable == null) cancelable = true;

    let dlog = new ProgressDialog(activity);
    dlog.setTitle(title);
    dlog.setMessage(message);
    dlog.setIndeterminate(true);
    dlog.setCancelable(cancelable);
    dlog.show();
    return dlog;
}

/**
 * 
 * @param {*进度条对话框标题} title 
 * @param {*进度条对话框是否可以强制关闭} cancelable 
 * @param {*进度条对话框的上限值} max 
 * @returns 返回对话框对象,可以通过一些方法实现关闭 自定义进度
 */
dialog.progress = function (title, cancelable, max) {
    if (max == null) max = 100;
    if (cancelable == null) cancelable = true;

    let dlog = new ProgressDialog(activity);
    dlog.setProgress(0);
    dlog.setTitle(title);
    dlog.setCancelable(cancelable);
    dlog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dlog.setMax(max);
    dlog.show();
    return dlog;
}

/**
 * 
 * @param {*输入对话框标题} title 
 * @param {*输入对话框的内容,可以为空} content 
 * @param {*是否可以强制关闭输入对话框} cancelable 
 * @param {*点击确定后调用的函数} fun 
 */
dialog.input = function (title, content, cancelable, fun) {
    if (cancelable == null) cancelable = true;
    if (!content) content = '';
    let editView = new EditText(activity);
    editView.setText(content);
    let dlog = new AlertDialog.Builder(activity);
    dlog.setTitle(title);
    dlog.setView(editView);
    dlog.setPositiveButton('确定', new DialogInterface.OnClickListener({
        onClick: function () {
            let text = editView.getText().toString();
            if (fun) fun(text);
        }
    }));
    dlog.show();
}

/**
 * 
 * @param {*自定义控件对话框标题} title 
 * @param {*自定义控件对话框内容,用到ui.infalte函数} v 
 * @param {*自定义控件对话框是否可以强制关闭} cancelable 
 * @param {*点击确定后调用的函数,唯一参数是自定义对象,可通过 参数.id名称获取控件内容} fun 
 */
dialog.customize = function (title, v, cancelable, bu1, bu2, bu3) {
    if (cancelable == null) cancelable = true;
    let dlog = new AlertDialog.Builder(activity);
    if (title) dlog.setTitle(title);

    let r = dlog.setView(v);

    dlog.setCancelable(cancelable);

    if (bu1) {
        dlog.setPositiveButton(bu1.text, new DialogInterface.OnClickListener({
            onClick: function () {
                if (bu1.fun) {
                    bu1.fun(r);
                }
            }
        }));
    }
    
    if (bu2) {
        dlog.setNegativeButton(bu2.text, new DialogInterface.OnClickListener({
            onClick: function () {
                if (bu2.fun) {
                    bu2.fun();
                }
            }
        }));
    }
    if (bu3) {
        dlog.setNeutralButton(bu3.text, new DialogInterface.OnClickListener({
            onClick: function () {
                if (bu3.fun) {
                    bu3.fun();
                }
            }
        }));
    }

    dlog.show();
}

dialog.datePicker = function (fun) {
    //获取当前日期
    let now_date = new Date()
    console.log(now_date.getDate())

    //年
    let m_year=now_date.getFullYear(), 
    //月
    m_month=now_date.getMonth(), 
    //日
    m_day=now_date.getDate(),
    //时间
    m_hour=12,
    m_minute=22;

    dateListener = new android.app.DatePickerDialog.OnDateSetListener({
        onDateSet : function (view, year, montOfYear, dayOfMonth) {
            m_year = year
            m_month = montOfYear
            m_day = dayOfMonth
            // console.log('123123')
            // console.log(m_day)
            fun({
                y : m_year,
                m : m_month,
                d : m_day
            })
        }
    })
    // console.log(d.state.get('activity'))
    let date = DatePickerDialog(activity, dateListener, m_year, m_month, m_day)
    date.setTitle('日期对话框')
    date.show()
}

dialog.timePicker = function  (fun) {
    
    //获取当前系统时间
    let d = new Date()

    //小时
    let h = d.getHours(),
    m = d.getMinutes()
    let tpd = new android.app.TimePickerDialog(activity, new android.app.TimePickerDialog.OnTimeSetListener({
        onTimeSet:function  (view, hourOfDay, minute) {
            console.log(hourOfDay)
        }
    }),h, m, true).show()

}
module.exports = dialog;